<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="900" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="auto" label-position="left">
			<el-row :gutter="20">
			  <el-col :span="24">
			  	<el-form-item label="头像" prop="avatar">
					<el-space wrap :size="8">
						<sc-upload v-model="form.avatar" round icon="el-icon-avatar" title="上传头像"></sc-upload>
					</el-space>
				</el-form-item>
			  </el-col>
			  <el-col :span="8">
			  	<el-form-item label="昵称" prop="nickname">
					<el-input v-model="form.nickname" clearable :min="1" :maxlangrh="20" placeholder="请输入昵称"></el-input>
				</el-form-item>
			  </el-col>
			  <el-col :span="8">
			  	<el-form-item label="姓名" prop="name">
					<el-input v-model="form.name" clearable :min="0" :maxlangrh="10" placeholder="请输入姓名"></el-input>
				</el-form-item>
			  </el-col>
			  <el-col :span="8">
				<el-form-item label="电话号码" prop="mobile">
					<el-input v-model="form.mobile" clearable :min="0" :maxlangrh="16" placeholder="请输入电话号码"></el-input>
				</el-form-item>
			  </el-col>
			  <el-col :span="8">
				<el-form-item label="状态" prop="status">
					<el-radio v-model="form.status" v-for="item in $parent.statusLabel" :key="item.value" 
					:label="item.value">{{item.label}}</el-radio>
				</el-form-item>
			  </el-col>
			  
			</el-row>
			
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				url: process.env.VUE_APP_FILES_URL,
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				parentSearch: {},
				citysOptions: [],
				businessDistrictsOptions: [],
				//表单数据
				form: {
					id: 0,
					avatar: '',
					name: '',
					nickname: "",
					mobile: "",
					status: 1,
				},
				//验证规则
				rules: {
					nickname: [
						{required: true, message: '请输入管家昵称'}
					],
				}
			}
		},
		mounted() {
			
		},
		methods: {
			//显示
			open(mode='add', search){
				this.parentSearch = search
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit() {
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.member.member.edit.post(this.form);
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.mode, this.parentSearch)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data) {
				this.form.id = data.id
				this.form.avatar = data.avatar
				this.form.name = data.name
				this.form.nickname = data.nickname
				this.form.mobile = data.mobile
				this.form.status = data.status

				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>
